﻿using IcsSEMCtrl.Db;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups
{
    public partial class VehicleManualMove : UIEditForm
    {
        LifterSEM lifterSEM = (LifterSEM)Naming.GetDeviceManager().GetLifter();
        public VehicleManualMove()
        {
            InitializeComponent();
            this.LoadVehicleDestFloor();
        }

        /// <summary>
        ///初始化加载轿厢目的地楼层
        /// </summary>
        private void LoadVehicleDestFloor()
        {
            List<string> floors = new List<string>();
            DBConnection dBConnection = DBControl.GetConnection();
            string sqlString = "SELECT DISTINCT  FLOOR FROM LIFTERPORT";
            try
            {
                SqlDataReader sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                while (sqlDataReader.Read())
                {
                    string floor = sqlDataReader["FLOOR"].ToString();
                    floors.Add(floor);
                }
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("在手动创建Move指令时，加载目的地数据报错",ex);
            }
            this.cbVehicleDestFloor.Items.AddRange(floors.ToArray());
        }

        /// <summary>
        /// 手动创建轿厢移动指令
        /// </summary>
        public void CreateManualMoveCommand()
        {
            //获取用户输入的目的地
            string moveDest = this.cbVehicleDestFloor.Text;
            if (string.IsNullOrEmpty(moveDest))
            {
                ShowWarningDialog("请选择轿厢移动目的地楼层");
            }
            else
            {
                //1.判断轿厢是否能创建Move指令
                bool flag = this.lifterSEM.ShuttleMaster.JudgeDoManualCommand();

                if (flag)
                {
                    bool returnCode = this.lifterSEM.ShuttleMaster.ManualCreateVehicelMoveCommand(moveDest);
                    if (returnCode)
                    {
                        ShowSuccessDialog("手动创建轿厢移动指令成功");
                        LogHelper.operationLogger($"用户{lifterSEM.UserName}进行了手动创建轿厢移动指令操作。");
                    }
                    else
                    {
                        ShowWarningDialog("手动创建轿厢移动指令失败");
                    }
                }
                else
                {
                    ShowWarningDialog("创建失败,设备状态为Down,或者设备运行模式不为Auto模式，或者轿厢状态为Down,或者轿厢搬送状态不为Ready状态,请确认！！");
                    LogHelper.loggerInfo("创建失败,设备状态为Down,或者设备运行模式不为Auto模式，或者轿厢状态为Down,或者轿厢搬送状态不为Ready状态");
                }
            }
        }
    }
}
